<?php

/***  DOCUMENTATION LAYER

	Array to Cols Function (Array Library)

	Last Update: Mar 2007
	Author: Tom at klenwell@gmail.com

	FUNCTIONS
	
		array_to_cols($ARRAY, $num_cols=1) 


  NOTES
		need to add sanity checks (is it array?  what about a min col length?)

______________________________________________________________________________*/


// array_to_cols
/*____________________________________________________________________________*/
function array_to_cols($ARRAY, $num_cols=1) 
{
// *** DATA

	// internal
	$_num_elements = count($ARRAY);
	$_COL_EL_COUNT = array();
	 
	// Return
	$COLS = array();

// *** MANIPULATE

	// Sanity Checks
	
	// divide up array elements
	for( $i = 0; $i < $num_cols; $i++ ) 
  {
  	$_COL_EL_COUNT[$i] = ceil($_num_elements/($num_cols-$i));
  	$_num_elements = $_num_elements - $_COL_EL_COUNT[$i];
  }
	
	// add elements to columns
	foreach ( $_COL_EL_COUNT as $_col_num => $_els_in_col ) 
  {
  	while( $_els_in_col > 0 ) 
  	{
			$_DATA = each($ARRAY);
			$COLS[$_col_num][$_DATA['key']] = $_DATA['value'];
  		$_els_in_col--;
  	}		
  }

// *** RETURN

	return $COLS;
} 
/*____________________________________________________________________________*/


// Testbed
/*____________________________________________________________________________*/


/*____________________________________________________________________________*/

?>
